package com.hand.qiandu.controller;

import com.hand.qiandu.annotation.PermissAnnotation;
import com.hand.qiandu.domain.Role;
import com.hand.qiandu.domain.User;
import com.hand.qiandu.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 24748  刘瑞锋
 * @date 2019/7/16
 */
@RestController
@PermissAnnotation(value = true)
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    IUserService userService;

    @PermissAnnotation(value = true)
    @GetMapping(value = "/lrf")
    public String test() {
        return "test";
    }

    @PermissAnnotation(value = true)
    @GetMapping(value = "/list")
    public List<User> getAllUser() {
        List<User> list = userService.queryAllUser();
        return list;
    }

    @GetMapping(value = "/{id}")
    User queryUser(@PathVariable("id") Long id){
        User user = userService.queryUser(id);
        return user;
    }

    @GetMapping(value = "/userRole/{id}")
    List<Role> queryUserRole(@PathVariable("id")Long id){
        return userService.queryUserRole(id);
    }

    @PostMapping(value = "/insert")
    User insertUser(User user){
        User us = null;
        us = userService.queryUser(user.getId().longValue());
        if(us == null){
            Long num = userService.insertUser(user);
            if(num == 1) {
                return userService.queryUser(user.getId().longValue());
            }
        }

        return us;
    }

    @PostMapping(value = "/update")
    User updateUser(User user) {
        User us = null;
        us = userService.queryUser(user.getId().longValue());
        if(us == null){
            userService.insertUser(user);
            return user;
        }
        userService.updateUser(user);
        return userService.queryUser(user.getId().longValue());
    }

    @GetMapping(value = "/delete/{id}")
    void deleteUser(@PathVariable("id")Long id){
        userService.deleteUser(id);
    }

    @GetMapping(value = "/selectAny/{ids}")
    List<User> queryUserByIds(@PathVariable("ids") List<Long> ids) {
        return userService.queryUserByIds(ids);
    }
}
